package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IColorDao;
import com.hamizhong.iflowers.db.entity.Color;
import com.hamizhong.iflowers.service.IColorService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.ColorVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class ColorServiceImpl extends MyBaseService implements IColorService {
	private IColorDao iColorDao = null;

	public IColorDao getiColorDao() {
		return iColorDao;
	}
	public void setiColorDao(IColorDao iColorDao) {
		this.iColorDao = iColorDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<Color> colorList = new ArrayList<Color>();
		colorList = iColorDao.findColor(pageNum, pageSize);
		int totalColorCnt = iColorDao.findTotalColorCnt();
		List<ColorVO> colorVOList = new ArrayList<ColorVO>();
		ColorVO colorVO = null;
		for (Color color : colorList) {
			colorVO = new ColorVO();
			BeanUtils.copyProperties(color, colorVO);
			colorVOList.add(colorVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setColorVOList(colorVOList);
		iObjectVO.setTotalColorCnt(totalColorCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addColor(ColorVO colorVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Color color = new Color();
		try {
			BeanUtils.copyProperties(colorVO, color);
			iColorDao.add(color);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updateColor(ColorVO colorVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Color color = new Color();
		try {
			BeanUtils.copyProperties(colorVO, color);
			iColorDao.update(color);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deleteColor(ColorVO colorVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			String colorId = colorVO.getColorId();
			Color color = iColorDao.findById(colorId);
			if(null != color) {
				this.iColorDao.delete(color);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
